import { OpenAI } from "openai";

const openai = new OpenAI({
  baseURL: 'https://api.deepseek.com',
  apiKey: process.env.OPENAI_API_KEY,
});

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const completion = await openai.chat.completions.create({
    messages: [
      { role: "system", content: "你是顶级Youtuber，根据已有的内容完成视频文案" },
      { role: "user", content: prompt }
    ],
    model: "deepseek-chat",
    stream: true,  // 启用流式响应
  });

  // 创建可读流
  const stream = new ReadableStream({
    async start(controller) {
      for await (const chunk of completion) {
        const content = chunk.choices[0]?.delta?.content || '';
        controller.enqueue(new TextEncoder().encode(content));
      }
      controller.close();
    },
  });

  return new Response(stream, {
    headers: {
      'Content-Type': 'text/plain',
    },
  });
}